<template>
	<view class="" style="height: 100vh;">
		<view class="bgfff" :style="{'paddingTop':statusBarHeigh + 'px',}">
			<view class="flex alicen pl20" :style="{'height':menuButtonInfo.height + 'px',}">
				<view class="w40b">
					<u-icon @click="goback(1)" name="arrow-left" size="20" color="#000"></u-icon>
				</view>
			</view>
		</view>
		<view class="h100b txtcen  colff ">
			<view class="bgfff mauto h100b  pl20 pr20 zdex9   ius10" v-if="isView === false">
				<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelAlign="right"
					:labelStyle="{'font-size':'14px','color':'black'}" errorType="none">
					<u-form-item prop="userInfo.phone" ref="item1" class="w60">
						<view class="flex alicen row">
							<u--input placeholder="请输入手机号" border="bottom" v-model="model1.userInfo.phone" type="number"
								maxlength="11" :readonly="isView" clearable></u--input>
						</view>
					</u-form-item>
					<u-form-item prop="userInfo.pwd" ref="item1" style="display:none">
						<view class="flex alicen row">
							<u--input placeholder="请输入您的自定义密码" border="bottom" v-model="model1.userInfo.pwd"
								type="password" :readonly="isView" clearable></u--input>
						</view>
					</u-form-item>
					<u-form-item prop="userInfo.yzm" borderBottom ref="item1" v-if="isModify === false">
						<view class="flex alicen row">
							<u--input placeholder="验证码" border="none" v-model="model1.userInfo.yzm" type="number"
								maxlength="6" :readonly="isView" clearable></u--input>
							<view class="fz28 col40" @tap="getCode()">获取验证码</view>
							<!-- <u-button @tap="getCode" :text="tips" type="primary" :disabled="disabledYzm"
								:customStyle="{'height':'35px','width':'40%','marginLeft':'10px'}"></u-button> -->
						</view>
					</u-form-item>
					<!--
					<u-form-item prop="userInfo.qggrade" borderBottom ref="item1">
						<view class="flex row alicen">
							<view class="nowrap fz28 ">请输入您的区内高考总分(含加分)</view>
							<view class="flex alicen ml10" style="border: 1rpx solid #ededed;">
								<u--input border="none" v-model="model1.userInfo.qggrade1" type="number" maxlength="3"
									:readonly="isView" clearable></u--input>
								<view style="padding: 0 1%;">分</view>
							</view>
						</view>
					</u-form-item>
					-->
					<u-form-item prop="userInfo.qggrade" borderBottom ref="item1">
						<view class="flex row alicen">
							<view class="nowrap fz28 ">请输入您的高考总分（含加分）</view>
							<view class="flex alicen ml10" style="border: 1rpx solid #ededed;">
								<u--input border="none" v-model="model1.userInfo.qggrade1" type="number" maxlength="3"
									:readonly="isView" clearable></u--input>
								<view style="padding: 0 1%;">分</view>
							</view>
						</view>
					</u-form-item>
					<u-form-item prop="userInfo.kmTmp" borderBottom ref="item1" labelPosition="top">
						<u-row>
							<u-col span="3">
								<view class="txtcen">首选科目</view>
							</u-col>
							<u-col span="9">
								<u-radio-group v-model="model1.userInfo.sxkmTmp" placement="row" :disabled="isView">
									<u-radio shape="circle" :customStyle="{marginRight: '16px'}"
										v-for="(item, index) in sxkmList" :key="index" :label="item.name"
										:name="item.name"></u-radio>
								</u-radio-group>
							</u-col>
						</u-row>
						<u-row class="mt20">
							<u-col span="3">
								<view class="txtcen">再选科目</view>
							</u-col>
							<u-col span="9">
								<u-checkbox-group v-model="model1.userInfo.zxkmTmp" placement="row" :disabled="isView">
									<u-checkbox shape="circle" :customStyle="{marginRight: '16px'}"
										v-for="(item, index) in zxkmList" :key="index" :label="item.name"
										:name="item.name"></u-checkbox>
								</u-checkbox-group>
							</u-col>
						</u-row>
					</u-form-item>
					<!-- <u-form-item label="" prop="userInfo.score" borderBottom ref="item1" style="display:none">
						<view class="flex row alicen">
							<u--input placeholder="请输入您的高考总分" border="surround" v-model="model1.userInfo.grade"
								type="number" maxlength="3" :readonly="isView" clearable></u--input>
							<view style="padding: 0 1%;">分</view>
						</view>
					</u-form-item> -->

					<u-form-item label="少数民族" prop="userInfo.sfssmz" borderBottom ref="item1" labelWidth='150'
						style="display:none">
						<u-radio-group v-model="model1.userInfo.sfssmz" placement="row">
							<u-radio :customStyle="{marginRight: '16px'}" v-for="(item, index) in sfssmzList"
								:key="index" :label="item.name" :name="item.value">
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="符合国家专项计划" prop="userInfo.sfgjzxdfzx" borderBottom ref="item1" labelWidth='150'
						style="display:none">
						<u-radio-group v-model="model1.userInfo.sfgjzxdfzx" placement="row">
							<u-radio :customStyle="{marginRight: '16px'}" v-for="(item, index) in sfssmzList"
								:key="index" :label="item.name" :name="item.value">
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="符合地方专项计划" prop="userInfo.sfdfzx" borderBottom ref="item1" labelWidth='150'
						style="display:none">
						<u-radio-group v-model="model1.userInfo.sfdfzx" placement="row">
							<u-radio :customStyle="{marginRight: '16px'}" v-for="(item, index) in sfssmzList"
								:key="index" :label="item.name" :name="item.value">
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<view class="fz24 nowrap mr20 colff7e5f">以下信息非必填，可<text class="ml10 col0000ff"
						@click="navswitch('/pages/keFuservice/keFuservice')">联系客服</text> 获取优惠</view>
					
					<u-form-item prop="userInfo.cardNo" borderBottom ref="item1">
						<view class="flex row alicen">
							<view class="nowrap fz28 ">会员卡号</view>
							<view class="flex alicen ml10" style="border: 1rpx solid #ededed;">
								<u--input border="none" v-model="model1.userInfo.cardNo" type="number" maxlength="25"
									:readonly="isView" clearable></u--input>
								<view style="padding: 0 1%;"></view>
							</view>
						</view>
					</u-form-item>
					<u-form-item prop="userInfo.cardPwd" borderBottom ref="item1">
						<view class="flex row alicen">
							<view class="nowrap fz28 ">激活密码</view>
							<view class="flex alicen ml10" style="border: 1rpx solid #ededed;">
								<u--input border="none" v-model="model1.userInfo.cardPwd" type="number" maxlength="25"
									:readonly="isView" clearable></u--input>
								<view style="padding: 0 1%;"></view>
							</view>
						</view>
					</u-form-item>
					
					
				</u--form>
				<view class="pb20"></view>
				
				
				
				<view class="flexCenter">
					<image class="w48 h48" @click="agree = !agree"
						:src="agree?cosImageUrl( 'check.png',true):cosImageUrl( 'nocheck.png',true)" mode="aspectFit">
					</image>
					<view class="fz24 ml10 txtleft col33">我已经阅读并同意 <text
							class="col40 ml10">《用户协议》《隐私协议》</text>若您的手机号未注册，将为您直接注册登录 </view>
				</view>
				<view class="pb60"></view>
				<view class="pt20 pb20 ius100 pt20 pb20 colff bgff000" @click="submitUserInfo">
					保存
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		province_list
	} from "../../utils/api/dict.js"
	import {
		loginMemberById,
		saveMember,
		sendSms,
		updateMember
	} from "../../utils/api/user.js"
	import jwx from '../../utils/jwx.js'
	export default {
		data() {
			return {
				agree: false,
				model1: {
					userInfo: {
						province: "广西",
						sxkm: "",
						zxkm: "",
						sxkmTmp: '',
						zxkmTmp: [],
						grade: null,
						qggrade1: null,
						qggrade2: null,
						gxgrade: null,
						sfssmz: 0,
						sfdfzx: 0,
						sfgjzxdfzx: 0
					}
				},
				userType: 0,
				isModify: false,
				isView: false,
				sxkmList: [{
						name: '历史',
						disabled: false
					},
					{
						name: '物理',
						disabled: false
					}
				],
				zxkmList: [{
						name: '化学',
						disabled: false
					},
					{
						name: '生物',
						disabled: false
					},
					{
						name: '地理',
						disabled: false
					},
					{
						name: '政治',
						disabled: false
					}
				],
				sfssmzList: [{
						name: '是',
						value: 1,
						disabled: false
					},
					{
						name: '否',
						value: 0,
						disabled: false
					}
				],
				popType: null,
				popTitle: null,
				showPop: false,
				popList: [],
				tips: "",
				disabledYzm: false
			}
		},
		methods: {
			async submitUserInfo() {
				let p = this.checkPhone(this.model1.userInfo.phone);
				if (!p.pass) return this.showMsg(p.msg)
				if (!this.agree) return this.showMsg('请同意用户协议和免责声明')
				if (!this.model1.userInfo.pwd) return this.showMsg('请输入自定义密码')
				if (!this.model1.userInfo.yzm) return this.showMsg('请输入验证码')
				if (!this.model1.userInfo.qggrade1) return this.showMsg('请输入您的高考总分（含加分）')
				//if (!this.model1.userInfo.qggrade2) return this.showMsg('请输入您的区内高考总分')
				if (!this.model1.userInfo.sxkmTmp) return this.showMsg('请选择首选科目')
				if (!this.model1.userInfo.zxkmTmp) return this.showMsg('请选择再选科目')
				//if (!this.model1.userInfo.sfssmz) return this.showMsg('请选择少数民族')
				 //if (!this.model1.userInfo.sfgjzxdfzx) return this.showMsg('请选择专项计划')
				let params = {
					phone: this.model1.userInfo.phone,
					pwd: this.model1.userInfo.pwd,
					yzm: this.model1.userInfo.yzm,
					gxgrade: this.model1.userInfo.qggrade1,
					qggrade:this.model1.userInfo.qggrade1,
					sxkm: this.model1.userInfo.sxkmTmp,
					zxkm: this.model1.userInfo.zxkmTmp.join(','),
					sfssmz: this.model1.userInfo.sfssmz,
					sfgjzxdfzx: this.model1.userInfo.sfgjzxdfzx,
					sfdfzx: this.model1.userInfo.sfdfzx,
					cardNo: this.model1.userInfo.cardNo,
					cardPwd: this.model1.userInfo.cardPwd
				}
				let res = await this.request.get('/api/saveMember', {
					params
				});
				if (res.code == 200) {
					
					let params = {
						useId: res.data.id
					}
					this.saveData('userdata', res.data)
					this.saveData('useId',res.data.id)
					let mit = await this.request.get('/api/showSubmit', {
						params
					});
					if (mit.code == 200) {
						this.successToShow(res.msg || '注册成功', call => {
							this.saveData('userall', res.data)
							this.navswitch('/pages/index/index')
						})
					}
				}
			},
			async getCode() {
				let p = this.checkPhone(this.model1.userInfo.phone);
				if (!p.pass) return this.showMsg(p.msg)
				let params = {
					mobile: this.model1.userInfo.phone,
					from: 'dny'
				}
				let res = await this.request.get('/api/sendSms', {
					params
				});
				if (res.code == 200) {
					this.successToShow(res.msg || '发送成功')
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			async loginMemberById(userId) {
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				var that = this
				that.userType = 0
				await loginMemberById({
					useId: userId
				}).then(res => {
					uni.hideLoading()
					if (res.code == 500) {
						that.isView = false
					} else {
						let temp = {
							phone: res.data.phone,
							grade: res.data.grade,
							qggrade: res.data.qggrade,
							gxgrade: res.data.gxgrade,
							sxkm: res.data.sxkm,
							zxkm: res.data.zxkm,
							sfssmz: res.data.sfssmz,
							sfgjzxdfzx: res.data.sfgjzxdfzx,
							sfdfzx: res.data.sfdfzx,
							cardNo: res.data.cardNo,
							cardPwd: res.data.cardPwd,
							yzm: null,
							pwd: res.data.pwd,
							id: res.data.id,
							openid: res.data.openid,
							province: res.data.province,
							sxkmTmp: res.data.sxkm,
							zxkmTmp: res.data.zxkm ? res.data.zxkm.split(",") : "",
							userType: res.data.userType ? res.data.userType : 0
						}
						that.isView = true
						that.$nextTick(() => {
							that.$set(that.model1, 'userInfo', temp)
						})
					}
				})
			},
			submit() {
				uni.navigateTo({
					url: '/pages/generateWish/generateWish'
				})
			},
			editUserInfo() {
				if (!this.model1.userInfo.province) {
					this.model1.userInfo.province = "广西"
				}
				// this.model1.userInfo.pwd = ""
				//暂时注释
				// if(this.userType === 1){
				this.isModify = true
				this.isView = false
				// }
			},
			updateUserInfo() {
				var that = this
				that.$refs.uForm.validate().then(res => {
					let saveObj = JSON.parse(JSON.stringify(that.model1.userInfo))
					saveObj.grade = saveObj.gxgrade
					saveObj.sxkm = saveObj.sxkmTmp
					saveObj.zxkm = saveObj.zxkmTmp.join(",")
					saveObj.openId = uni.getStorageSync("openid")
					saveObj.ruser = that.$store.state.ruser ? that.$store.state.ruser : ""
					delete saveObj.sxkmTmp
					delete saveObj.zxkmTmp
					updateMember(saveObj).then(res => {
						if (res.code == 200) {
							that.$func.showToast(res.msg, 3000, true)
							that.isView = true
							that.isModify = false
							setTimeout(() => {
								that.loginMemberById(that.$store.state.userId)
							}, 4000)
						} else {
							that.$func.showToast(res.msg, 3000, true)
						}
					})
				}).catch(errors => {
					uni.$u.toast('请检查填写项目')
				})
			},

		},
		watch: {
			"$store.state.userId": function(val, oldval) {
				if (val) {
					this.loginMemberById(val)
				}
			}
		},
		async onShow() {
			let userId = this.$store.state.userId
			if (userId) {
				await this.loginMemberById(userId)
			}
		}
	}
</script>

<style lang="less" scoped>
	/* 底部安全边距 start*/
</style>